#ifdef __MIIPTS__
#include "ven_sdk.h"
#include "ven_handle.h"
#include "ven_graphics.h"
#include "ven_stc.h"
#include "ven_ts.h"
#include "ven_os.h"
#include "ven_kpd.h"
#include "ven_setting.h"
#include "ven_clipboard.h"
#include "ven_dbg.h"
#include "ven_drv.h"
#include "ven_codec.h"
//c #include "ven_socket.h"
#include "ven_util.h"

#include "mbm_typedefine.h"
#include "mbm_mstar_inc.h"
#include "Mbm_vui_adapter.h"
#include "Mbm_am_adapter.h"
#include "Mbm_av_adapter.h"
#include "Mbm_sys_adapter.h"
#include "Mbm_account_adapter.h"
#include "Imibase.h"

//#include "imp_io_wrapper.h"

UInt32   miipts_ven_handle;
Int32 	gImibaseWinId = 0;
UInt32  	g_miiMediaHandle = 0; 

void imichat_active_networkd_and_startup(void);

void miipts_startVendor(void)
{
	ven_kpd_subscribe(miipts_ven_handle, mbm_am_key_handler);
	ven_ts_subscribe(miipts_ven_handle, mbm_am_scr_reg_tp_handler);
	
#ifdef __SDK_MMI_DISPLAY_MANAGER__
	// Enable to update screen
	ven_graphics_displayManagerEnableUpdate((u32)E_VEN_MIIPTS);
#endif	
}

int  miipts_media_open(void)
{
    ven_codec_ret_t ret = VEN_CODEC_SUCCESS;
    ven_req_data_t ReqData={0};
	
    if(g_miiMediaHandle)
        return VEN_CODEC_SUCCESS;


    ReqData.Handle   = miipts_ven_handle;
    ReqData.pfnCb    = NULL;
    ReqData.bSync    = TRUE;
    ReqData.userData = CODEC_OPEN_MEDIA;

    ret = ven_codec_openMedia((s32*)&g_miiMediaHandle, &ReqData);

    return ret;
}


int  miipts_media_close(void)
{
    ven_codec_ret_t ret = VEN_CODEC_SUCCESS;
    ven_req_data_t ReqData={0};

    //already closed!
    if(!g_miiMediaHandle)
        return VEN_CODEC_SUCCESS;

    ReqData.Handle      = miipts_ven_handle;
    ReqData.pfnCb       = NULL;
    ReqData.bSync       = TRUE;
    ReqData.userData    = CODEC_CLOSE_MEDIA;

    ret = ven_codec_closeMedia(g_miiMediaHandle, &ReqData);

    //Clear handle regardless of close action succeed or fail!
    g_miiMediaHandle = 0;

    return ret;
}

void miipts_ven_set_interrupt()
{
	ven_req_data_t tVenReq = {0};

	tVenReq.Handle      = miipts_ven_handle;
	tVenReq.bSync       = TRUE;
	tVenReq.pfnCb       = NULL;
	tVenReq.userData    = 0;

	ven_ui_setInterruptLevel(VEN_UI_INT_DENY_USB|VEN_UI_INT_DENY_BT_OBEX/*|VEN_UI_INT_DENY_MT*/ , &tVenReq);
}
void miipts_ven_start(void *pData, u32 len)
#if 0
{
	ven_rsp_data_t tVenReq;

	mbm_trace("miipts_ven_start(0x%08x, %d): %s, %d", pData, len, __FILE__, __LINE__);
	
	tVenReq.Handle = miipts_ven_handle;
	tVenReq.retData = VEN_STC_SUCCESS;
 
	ven_stc_startRsp(&tVenReq);

	miipts_startVendor();
//	mbm_sys_adp_init();

	if(gImibaseWinId)
    {
		mbm_trace("miipts_ven_start get focus");
		mbm_am_excute_state_handler(MBM_APPLET_FOREGROUND);
		return;
    }

	mbm_start_imibase_window();

	miipts_media_open();

	miipts_ven_set_interrupt();
	if (1) //c (pData && (len > 0))
	{
		mbm_trace("miipts_ven_start  nStartParam=%s",(UInt8*)pData);

		//c if(!ven_std_memcmp("1", pData, sizeof("1")))
		{
			gImibaseWinId = 1; //c SCR_ID_MBM_IMIBASE;
			mbm_trace("mbm_start_imibase_window imibase_startup");
			imibase_startup("2", 0);
		}
	
	}
	else
	{
		mbm_trace("miipts_ven_start bad input param");
	}
			
}
#else
{
	ven_rsp_data_t tVenReq;

	mbm_trace("miipts_ven_start(0x%08x, %d): %s, %d", pData, len, __FILE__, __LINE__);
//	mbm_trace("miipts_ven_start");

	
	tVenReq.Handle = miipts_ven_handle;
	tVenReq.retData = VEN_STC_SUCCESS;
 
	ven_stc_startRsp(&tVenReq);

	miipts_startVendor();
//	mbm_sys_adp_init();

	if(gImibaseWinId)
	{
		mbm_trace("miipts_ven_start get focus");
		mbm_am_excute_state_handler(MBM_APPLET_FOREGROUND);
		return;
	}

	mbm_start_imibase_window();

	miipts_media_open();
    
	miipts_ven_set_interrupt();

//	imp_net_test();
//	imp_io_test();	

//	imp_sysparam_test();

//	imp_vui_test(mbm_GetLCDFrameBuffer(NULL));
	
//	imp_mw_test("/CARD/applet.exe");
    
    imichat_active_networkd_and_startup();
    
//    imichat_startup();
//	imp_png_test("/CARD/1.png", "/CARD/2.bmp");
	
//	imp_mw_test("/CARD/applet.exe");
	
//	mbm_Display_DirectDrawImg(0, 0, 240, 320, 0);
}
#endif

void miipts_stopVendor(void)
{
	mbm_trace("miipts_stopVendor");

	ven_ts_unsubscribe(miipts_ven_handle);
	ven_kpd_unsubscribe(miipts_ven_handle);
	
#ifdef __SDK_MMI_DISPLAY_MANAGER__
	// Diable to update screen
	ven_graphics_displayManagerDisableUpdate();
#endif	
}

void miipts_ven_exit(void)
{
	miipts_stopVendor();
	
	gImibaseWinId = 0;
//	imibase_exit();
//    mbm_network_deactive();
}

static void miipts_ven_stop(void)
{
	ven_rsp_data_t tVenReq;

	mbm_trace("miipts_ven_stop");
	
	miipts_ven_exit();
	
	tVenReq.Handle = miipts_ven_handle;
	tVenReq.retData = VEN_STC_SUCCESS;
	
	ven_stc_stopRsp(&tVenReq);
}

void miipts_suspendVendor(void)
{
	mbm_trace("miipts_suspendVendor");

    /* unsubscribe key event */
    ven_kpd_unsubscribe(miipts_ven_handle);

    /* unsubscribe touch event */
    ven_ts_unsubscribe(miipts_ven_handle);

	
#ifdef __SDK_MMI_DISPLAY_MANAGER__
	// Diable to update screen
	ven_graphics_displayManagerDisableUpdate();
#endif	
}


static void miipts_ven_suspend(void)
{
    ven_rsp_data_t tVenRSp;

    /* Do suspend here */
    miipts_suspendVendor();

    tVenRSp.Handle = miipts_ven_handle;
    tVenRSp.retData = VEN_STC_SUCCESS;

    ven_stc_suspendRsp(&tVenRSp);

}

void miipts_resumeVendor(void)
{
	mbm_trace("miipts_resumeVendor");

    /* unsubscribe key event */
    ven_kpd_unsubscribe(miipts_ven_handle);

    /* unsubscribe touch event */
    ven_ts_unsubscribe(miipts_ven_handle);

	
#ifdef __SDK_MMI_DISPLAY_MANAGER__
	// Diable to update screen
	ven_graphics_displayManagerDisableUpdate();
#endif	
}

static void miipts_ven_resume(void)
{
    ven_rsp_data_t tVenRSp;

    /* Do resume here */
    miipts_resumeVendor();

    tVenRSp.Handle = miipts_ven_handle;
    tVenRSp.retData = VEN_STC_SUCCESS;

    ven_stc_resumeRsp(&tVenRSp);
}


void miipts_ven_init(void)	
{
	ven_stc_ctrl_func_t miiFunc = {
		miipts_ven_start,
		miipts_ven_stop,
		miipts_ven_suspend,
		miipts_ven_resume,
		NULL
		//miipts_ven_statusInd
	};
	
    miipts_ven_handle = ven_handle_open(E_VEN_MIIPTS);

	mbm_trace("miipts_ven_init: miipts_ven_handle = %d", miipts_ven_handle);

	ven_stc_regFuncs(miipts_ven_handle, &miiFunc);

}

void imibase_exit(void)
{
}


#endif

